home *** CD-ROM | disk | FTP | other *** search
/ Software of the Month Club 2000 October / Software of the Month - Ultimate Collection Shareware 277.iso / pc / PROGRAMS / UTILITY / WINLINUX / DATA1.CAB / programs_-_kernel_source / FS / LOCKS.C < prev    next >
Encoding:
C/C++ Source or Header  |  1999-09-17  |  32.9 KB  |  1,258 lines

  1. /*
  2.  *  linux/fs/locks.c
  3.  *
  4.  *  Provide support for fcntl()'s F_GETLK, F_SETLK, and F_SETLKW calls.
  5.  *  Doug Evans (dje@spiff.uucp), August 07, 1992
  6.  *
  7.  *  Deadlock detection added.
  8.  *  FIXME: one thing isn't handled yet:
  9.  *    - mandatory locks (requires lots of changes elsewhere)
  10.  *  Kelly Carmichael (kelly@[142.24.8.65]), September 17, 1994.
  11.  *
  12.  *  Miscellaneous edits, and a total rewrite of posix_lock_file() code.
  13.  *  Kai Petzke (wpp@marie.physik.tu-berlin.de), 1994
  14.  *  
  15.  *  Converted file_lock_table to a linked list from an array, which eliminates
  16.  *  the limits on how many active file locks are open.
  17.  *  Chad Page (pageone@netcom.com), November 27, 1994
  18.  * 
  19.  *  Removed dependency on file descriptors. dup()'ed file descriptors now
  20.  *  get the same locks as the original file descriptors, and a close() on
  21.  *  any file descriptor removes ALL the locks on the file for the current
  22.  *  process. Since locks still depend on the process id, locks are inherited
  23.  *  after an exec() but not after a fork(). This agrees with POSIX, and both
  24.  *  BSD and SVR4 practice.
  25.  *  Andy Walker (andy@lysaker.kvaerner.no), February 14, 1995
  26.  *
  27.  *  Scrapped free list which is redundant now that we allocate locks
  28.  *  dynamically with kmalloc()/kfree().
  29.  *  Andy Walker (andy@lysaker.kvaerner.no), February 21, 1995
  30.  *
  31.  *  Implemented two lock personalities - FL_FLOCK and FL_POSIX.
  32.  *
  33.  *  FL_POSIX locks are created with calls to fcntl() and lockf() through the
  34.  *  fcntl() system call. They have the semantics described above.
  35.  *
  36.  *  FL_FLOCK locks are created with calls to flock(), through the flock()
  37.  *  system call, which is new. Old C libraries implement flock() via fcntl()
  38.  *  and will continue to use the old, broken implementation.
  39.  *
  40.  *  FL_FLOCK locks follow the 4.4 BSD flock() semantics. They are associated
  41.  *  with a file pointer (filp). As a result they can be shared by a parent
  42.  *  process and its children after a fork(). They are removed when the last
  43.  *  file descriptor referring to the file pointer is closed (unless explicitly
  44.  *  unlocked). 
  45.  *
  46.  *  FL_FLOCK locks never deadlock, an existing lock is always removed before
  47.  *  upgrading from shared to exclusive (or vice versa). When this happens
  48.  *  any processes blocked by the current lock are woken up and allowed to
  49.  *  run before the new lock is applied.
  50.  *  Andy Walker (andy@lysaker.kvaerner.no), June 09, 1995
  51.  *
  52.  *  Removed some race conditions in flock_lock_file(), marked other possible
  53.  *  races. Just grep for FIXME to see them. 
  54.  *  Dmitry Gorodchanin (pgmdsg@ibi.com), February 09, 1996.
  55.  *
  56.  *  Addressed Dmitry's concerns. Deadlock checking no longer recursive.
  57.  *  Lock allocation changed to GFP_ATOMIC as we can't afford to sleep
  58.  *  once we've checked for blocking and deadlocking.
  59.  *  Andy Walker (andy@lysaker.kvaerner.no), April 03, 1996.
  60.  *
  61.  *  Initial implementation of mandatory locks. SunOS turned out to be
  62.  *  a rotten model, so I implemented the "obvious" semantics.
  63.  *  See 'linux/Documentation/mandatory.txt' for details.
  64.  *  Andy Walker (andy@lysaker.kvaerner.no), April 06, 1996.
  65.  *
  66.  *  Don't allow mandatory locks on mmap()'ed files. Added simple functions to
  67.  *  check if a file has mandatory locks, used by mmap(), open() and creat() to
  68.  *  see if system call should be rejected. Ref. HP-UX/SunOS/Solaris Reference
  69.  *  Manual, Section 2.
  70.  *  Andy Walker (andy@lysaker.kvaerner.no), April 09, 1996.
  71.  *
  72.  *  Tidied up block list handling. Added '/proc/locks' interface.
  73.  *  Andy Walker (andy@lysaker.kvaerner.no), April 24, 1996.
  74.  *
  75.  *  Fixed deadlock condition for pathological code that mixes calls to
  76.  *  flock() and fcntl().
  77.  *  Andy Walker (andy@lysaker.kvaerner.no), April 29, 1996.
  78.  *
  79.  *  Allow only one type of locking scheme (FL_POSIX or FL_FLOCK) to be in use
  80.  *  for a given file at a time. Changed the CONFIG_LOCK_MANDATORY scheme to
  81.  *  guarantee sensible behaviour in the case where file system modules might
  82.  *  be compiled with different options than the kernel itself.
  83.  *  Andy Walker (andy@lysaker.kvaerner.no), May 15, 1996.
  84.  *
  85.  *  Added a couple of missing wake_up() calls. Thanks to Thomas Meckel
  86.  *  (Thomas.Meckel@mni.fh-giessen.de) for spotting this.
  87.  *  Andy Walker (andy@lysaker.kvaerner.no), May 15, 1996.
  88.  *
  89.  *  Changed FL_POSIX locks to use the block list in the same way as FL_FLOCK
  90.  *  locks. Changed process synchronisation to avoid dereferencing locks that
  91.  *  have already been freed.
  92.  *  Andy Walker (andy@lysaker.kvaerner.no), Sep 21, 1996.
  93.  *
  94.  *  Made the block list a circular list to minimise searching in the list.
  95.  *  Andy Walker (andy@lysaker.kvaerner.no), Sep 25, 1996.
  96.  *
  97.  *  Made mandatory locking a mount option. Default is not to allow mandatory
  98.  *  locking.
  99.  *  Andy Walker (andy@lysaker.kvaerner.no), Oct 04, 1996.
  100.  *
  101.  *  Some adaptations for NFS support.
  102.  *  Olaf Kirch (okir@monad.swb.de), Dec 1996,
  103.  *
  104.  *  Fixed /proc/locks interface so that we can't overrun the buffer we are handed.
  105.  *  Andy Walker (andy@lysaker.kvaerner.no), May 12, 1997.
  106.  */
  107.  
  108. #include <linux/malloc.h>
  109. #include <linux/file.h>
  110. #include <linux/smp_lock.h>
  111.  
  112. #include <asm/uaccess.h>
  113.  
  114. #define OFFSET_MAX    ((off_t)LONG_MAX)    /* FIXME: move elsewhere? */
  115.  
  116. static int flock_make_lock(struct file *filp, struct file_lock *fl,
  117.                    unsigned int cmd);
  118. static int posix_make_lock(struct file *filp, struct file_lock *fl,
  119.                    struct flock *l);
  120. static int flock_locks_conflict(struct file_lock *caller_fl,
  121.                 struct file_lock *sys_fl);
  122. static int posix_locks_conflict(struct file_lock *caller_fl,
  123.                 struct file_lock *sys_fl);
  124. static int locks_conflict(struct file_lock *caller_fl, struct file_lock *sys_fl);
  125. static int flock_lock_file(struct file *filp, struct file_lock *caller,
  126.                unsigned int wait);
  127. static int posix_locks_deadlock(struct file_lock *caller,
  128.                 struct file_lock *blocker);
  129.  
  130. static struct file_lock *locks_empty_lock(void);
  131. static struct file_lock *locks_init_lock(struct file_lock *,
  132.                      struct file_lock *);
  133. static void locks_insert_lock(struct file_lock **pos, struct file_lock *fl);
  134. static void locks_delete_lock(struct file_lock **thisfl_p, unsigned int wait);
  135. static char *lock_get_status(struct file_lock *fl, int id, char *pfx);
  136.  
  137. static void locks_insert_block(struct file_lock *blocker, struct file_lock *waiter);
  138. static void locks_delete_block(struct file_lock *blocker, struct file_lock *waiter);
  139. static void locks_wake_up_blocks(struct file_lock *blocker, unsigned int wait);
  140.  
  141. struct file_lock *file_lock_table = NULL;
  142.  
  143. /* Allocate a new lock, and initialize its fields from fl.
  144.  * The lock is not inserted into any lists until locks_insert_lock() or 
  145.  * locks_insert_block() are called.
  146.  */
  147. static inline struct file_lock *locks_alloc_lock(struct file_lock *fl)
  148. {
  149.     return locks_init_lock(locks_empty_lock(), fl);
  150. }
  151.  
  152. /* Free lock not inserted in any queue.
  153.  */
  154. static inline void locks_free_lock(struct file_lock *fl)
  155. {
  156.     if (waitqueue_active(&fl->fl_wait))
  157.         panic("Attempting to free lock with active wait queue");
  158.  
  159.     if (fl->fl_nextblock != NULL || fl->fl_prevblock != NULL)
  160.         panic("Attempting to free lock with active block list");
  161.         
  162.     kfree(fl);
  163.     return;
  164. }
  165.  
  166. /* Check if two locks overlap each other.
  167.  */
  168. static inline int locks_overlap(struct file_lock *fl1, struct file_lock *fl2)
  169. {
  170.     return ((fl1->fl_end >= fl2->fl_start) &&
  171.         (fl2->fl_end >= fl1->fl_start));
  172. }
  173.  
  174. /*
  175.  * Check whether two locks have the same owner
  176.  * N.B. Do we need the test on PID as well as owner?
  177.  * (Clone tasks should be considered as one "owner".)
  178.  */
  179. static inline int
  180. locks_same_owner(struct file_lock *fl1, struct file_lock *fl2)
  181. {
  182.     return (fl1->fl_owner == fl2->fl_owner) &&
  183.            (fl1->fl_pid   == fl2->fl_pid);
  184. }
  185.  
  186. /* Insert waiter into blocker's block list.
  187.  * We use a circular list so that processes can be easily woken up in
  188.  * the order they blocked. The documentation doesn't require this but
  189.  * it seems like the reasonable thing to do.
  190.  */
  191. static void locks_insert_block(struct file_lock *blocker, 
  192.                    struct file_lock *waiter)
  193. {
  194.     struct file_lock *prevblock;
  195.  
  196.     if (blocker->fl_prevblock == NULL)
  197.         /* No previous waiters - list is empty */
  198.         prevblock = blocker;
  199.     else
  200.         /* Previous waiters exist - add to end of list */
  201.         prevblock = blocker->fl_prevblock;
  202.  
  203.     prevblock->fl_nextblock = waiter;
  204.     blocker->fl_prevblock = waiter;
  205.     waiter->fl_nextblock = blocker;
  206.     waiter->fl_prevblock = prevblock;
  207.     
  208.     return;
  209. }
  210.  
  211. /* Remove waiter from blocker's block list.
  212.  * When blocker ends up pointing to itself then the list is empty.
  213.  */
  214. static void locks_delete_block(struct file_lock *blocker,
  215.                    struct file_lock *waiter)
  216. {
  217.     struct file_lock *nextblock;
  218.     struct file_lock *prevblock;
  219.     
  220.     nextblock = waiter->fl_nextblock;
  221.     prevblock = waiter->fl_prevblock;
  222.  
  223.     if (nextblock == NULL)
  224.         return;
  225.     
  226.     nextblock->fl_prevblock = prevblock;
  227.     prevblock->fl_nextblock = nextblock;
  228.  
  229.     waiter->fl_prevblock = waiter->fl_nextblock = NULL;
  230.     if (blocker->fl_nextblock == blocker)
  231.         /* No more locks on blocker's blocked list */
  232.         blocker->fl_prevblock = blocker->fl_nextblock = NULL;
  233.     return;
  234. }
  235.  
  236. /* The following two are for the benefit of lockd.
  237.  */
  238. void
  239. posix_block_lock(struct file_lock *blocker, struct file_lock *waiter)
  240. {
  241.     locks_insert_block(blocker, waiter);
  242.     return;
  243. }
  244.  
  245. void
  246. posix_unblock_lock(struct file_lock *waiter)
  247. {
  248.     if (waiter->fl_prevblock)
  249.         locks_delete_block(waiter->fl_prevblock, waiter);
  250.     return;
  251. }
  252.  
  253. /* Wake up processes blocked waiting for blocker.
  254.  * If told to wait then schedule the processes until the block list
  255.  * is empty, otherwise empty the block list ourselves.
  256.  */
  257. static void locks_wake_up_blocks(struct file_lock *blocker, unsigned int wait)
  258. {
  259.     struct file_lock *waiter;
  260.  
  261.     while ((waiter = blocker->fl_nextblock) != NULL) {
  262.         /* N.B. Is it possible for the notify function to block?? */
  263.         if (waiter->fl_notify)
  264.             waiter->fl_notify(waiter);
  265.         wake_up(&waiter->fl_wait);
  266.         if (wait)
  267.             /* Let the blocked process remove waiter from the
  268.              * block list when it gets scheduled.
  269.              */
  270.             schedule();
  271.         else
  272.             /* Remove waiter from the block list, because by the
  273.              * time it wakes up blocker won't exist any more.
  274.              */
  275.             locks_delete_block(blocker, waiter);
  276.     }
  277.     return;
  278. }
  279.  
  280. /* flock() system call entry point. Apply a FL_FLOCK style lock to
  281.  * an open file descriptor.
  282.  */
  283. asmlinkage int sys_flock(unsigned int fd, unsigned int cmd)
  284. {
  285.     struct file_lock file_lock;
  286.     struct file *filp;
  287.     int error;
  288.  
  289.     lock_kernel();
  290.     error = -EBADF;
  291.     filp = fget(fd);
  292.     if (!filp)
  293.         goto out;
  294.     error = -EINVAL;
  295.     if (!flock_make_lock(filp, &file_lock, cmd))
  296.         goto out_putf;
  297.     error = -EBADF;
  298.     if ((file_lock.fl_type != F_UNLCK) && !(filp->f_mode & 3))
  299.         goto out_putf;
  300.     error = flock_lock_file(filp, &file_lock,
  301.                 (cmd & (LOCK_UN | LOCK_NB)) ? 0 : 1);
  302. out_putf:
  303.     fput(filp);
  304. out:
  305.     unlock_kernel();
  306.     return (error);
  307. }
  308.  
  309. /* Report the first existing lock that would conflict with l.
  310.  * This implements the F_GETLK command of fcntl().
  311.  */
  312. int fcntl_getlk(unsigned int fd, struct flock *l)
  313. {
  314.     struct file *filp;
  315.     struct file_lock *fl,file_lock;
  316.     struct flock flock;
  317.     int error;
  318.  
  319.     error = -EFAULT;
  320.     if (copy_from_user(&flock, l, sizeof(flock)))
  321.         goto out;
  322.     error = -EINVAL;
  323.     if ((flock.l_type != F_RDLCK) && (flock.l_type != F_WRLCK))
  324.         goto out;
  325.  
  326.     error = -EBADF;
  327.     filp = fget(fd);
  328.     if (!filp)
  329.         goto out;
  330.  
  331.     error = -EINVAL;
  332.     if (!filp->f_dentry || !filp->f_dentry->d_inode)
  333.         goto out_putf;
  334.  
  335.     if (!posix_make_lock(filp, &file_lock, &flock))
  336.         goto out_putf;
  337.  
  338.     if (filp->f_op->lock) {
  339.         error = filp->f_op->lock(filp, F_GETLK, &file_lock);
  340.         if (error < 0)
  341.             goto out_putf;
  342.         fl = &file_lock;
  343.     } else {
  344.         fl = posix_test_lock(filp, &file_lock);
  345.     }
  346.  
  347.     flock.l_type = F_UNLCK;
  348.     if (fl != NULL) {
  349.         flock.l_pid = fl->fl_pid;
  350.         flock.l_start = fl->fl_start;
  351.         flock.l_len = fl->fl_end == OFFSET_MAX ? 0 :
  352.             fl->fl_end - fl->fl_start + 1;
  353.         flock.l_whence = 0;
  354.         flock.l_type = fl->fl_type;
  355.     }
  356.     error = -EFAULT;
  357.     if (!copy_to_user(l, &flock, sizeof(flock)))
  358.         error = 0;
  359.   
  360. out_putf:
  361.     fput(filp);
  362. out:
  363.     return error;
  364. }
  365.  
  366. /* Apply the lock described by l to an open file descriptor.
  367.  * This implements both the F_SETLK and F_SETLKW commands of fcntl().
  368.  */
  369. int fcntl_setlk(unsigned int fd, unsigned int cmd, struct flock *l)
  370. {
  371.     struct file *filp;
  372.     struct file_lock file_lock;
  373.     struct flock flock;
  374.     struct dentry * dentry;
  375.     struct inode *inode;
  376.     int error;
  377.  
  378.     /*
  379.      * This might block, so we do it before checking the inode.
  380.      */
  381.     error = -EFAULT;
  382.     if (copy_from_user(&flock, l, sizeof(flock)))
  383.         goto out;
  384.  
  385.     /* Get arguments and validate them ...
  386.      */
  387.  
  388.     error = -EBADF;
  389.     filp = fget(fd);
  390.     if (!filp)
  391.         goto out;
  392.  
  393.     error = -EINVAL;
  394.     if (!(dentry = filp->f_dentry))
  395.         goto out_putf;
  396.     if (!(inode = dentry->d_inode))
  397.         goto out_putf;
  398.  
  399.     /* Don't allow mandatory locks on files that may be memory mapped
  400.      * and shared.
  401.      */
  402.     if (IS_MANDLOCK(inode) &&
  403.         (inode->i_mode & (S_ISGID | S_IXGRP)) == S_ISGID &&
  404.         inode->i_mmap) {
  405.         struct vm_area_struct *vma = inode->i_mmap;
  406.         error = -EAGAIN;
  407.         do {
  408.             if (vma->vm_flags & VM_MAYSHARE)
  409.                 goto out_putf;
  410.         } while ((vma = vma->vm_next_share) != NULL);
  411.     }
  412.  
  413.     error = -EINVAL;
  414.     if (!posix_make_lock(filp, &file_lock, &flock))
  415.         goto out_putf;
  416.     
  417.     error = -EBADF;
  418.     switch (flock.l_type) {
  419.     case F_RDLCK:
  420.         if (!(filp->f_mode & FMODE_READ))
  421.             goto out_putf;
  422.         break;
  423.     case F_WRLCK:
  424.         if (!(filp->f_mode & FMODE_WRITE))
  425.             goto out_putf;
  426.         break;
  427.     case F_UNLCK:
  428.         break;
  429.     case F_SHLCK:
  430.     case F_EXLCK:
  431. #ifdef __sparc__
  432. /* warn a bit for now, but don't overdo it */
  433. {
  434.     static int count = 0;
  435.     if (!count) {
  436.         count=1;
  437.         printk(KERN_WARNING
  438.                "fcntl_setlk() called by process %d (%s) with broken flock() emulation\n",
  439.                current->pid, current->comm);
  440.     }
  441. }
  442.         if (!(filp->f_mode & 3))
  443.             goto out_putf;
  444.         break;
  445. #endif
  446.     default:
  447.         error = -EINVAL;
  448.         goto out_putf;
  449.     }
  450.  
  451.     if (filp->f_op->lock != NULL) {
  452.         error = filp->f_op->lock(filp, cmd, &file_lock);
  453.         if (error < 0)
  454.             goto out_putf;
  455.     }
  456.     error = posix_lock_file(filp, &file_lock, cmd == F_SETLKW);
  457.  
  458. out_putf:
  459.     fput(filp);
  460. out:
  461.     return error;
  462. }
  463.  
  464. /*
  465.  * This function is called when the file is being removed
  466.  * from the task's fd array.
  467.  */
  468. void locks_remove_posix(struct file *filp, fl_owner_t owner)
  469. {
  470.     struct inode * inode = filp->f_dentry->d_inode;
  471.     struct file_lock file_lock, *fl;
  472.     struct file_lock **before;
  473.  
  474.     /*
  475.      * For POSIX locks we free all locks on this file for the given task.
  476.      */
  477. repeat:
  478.     before = &inode->i_flock;
  479.     while ((fl = *before) != NULL) {
  480.         if ((fl->fl_flags & FL_POSIX) && fl->fl_owner == owner) {
  481.             int (*lock)(struct file *, int, struct file_lock *);
  482.             lock = filp->f_op->lock;
  483.             if (lock) {
  484.                 file_lock = *fl;
  485.                 file_lock.fl_type = F_UNLCK;
  486.             }
  487.             locks_delete_lock(before, 0);
  488.             if (lock) {
  489.                 lock(filp, F_SETLK, &file_lock);
  490.                 /* List may have changed: */
  491.                 goto repeat;
  492.             }
  493.             continue;
  494.         }
  495.         before = &fl->fl_next;
  496.     }
  497. }
  498.  
  499. /*
  500.  * This function is called on the last close of an open file.
  501.  */
  502. void locks_remove_flock(struct file *filp)
  503. {
  504.     struct inode * inode = filp->f_dentry->d_inode; 
  505.     struct file_lock file_lock, *fl;
  506.     struct file_lock **before;
  507.  
  508. repeat:
  509.     before = &inode->i_flock;
  510.     while ((fl = *before) != NULL) {
  511.         if ((fl->fl_flags & FL_FLOCK) && fl->fl_file == filp) {
  512.             int (*lock)(struct file *, int, struct file_lock *);
  513.             lock = filp->f_op->lock;
  514.             if (lock) {
  515.                 file_lock = *fl;
  516.                 file_lock.fl_type = F_UNLCK;
  517.             }
  518.             locks_delete_lock(before, 0);
  519.             if (lock) {
  520.                 lock(filp, F_SETLK, &file_lock);
  521.                 /* List may have changed: */
  522.                 goto repeat;
  523.             }
  524.             continue;
  525.         }
  526.         before = &fl->fl_next;
  527.     }
  528. }
  529.  
  530. struct file_lock *
  531. posix_test_lock(struct file *filp, struct file_lock *fl)
  532. {
  533.     struct file_lock *cfl;
  534.  
  535.     for (cfl = filp->f_dentry->d_inode->i_flock; cfl; cfl = cfl->fl_next) {
  536.         if (!(cfl->fl_flags & FL_POSIX))
  537.             continue;
  538.         if (posix_locks_conflict(cfl, fl))
  539.             break;
  540.     }
  541.  
  542.     return (cfl);
  543. }
  544.  
  545. int locks_verify_locked(struct inode *inode)
  546. {
  547.     /* Candidates for mandatory locking have the setgid bit set
  548.      * but no group execute bit -  an otherwise meaningless combination.
  549.      */
  550.     if (IS_MANDLOCK(inode) &&
  551.         (inode->i_mode & (S_ISGID | S_IXGRP)) == S_ISGID)
  552.         return (locks_mandatory_locked(inode));
  553.     return (0);
  554. }
  555.  
  556. int locks_verify_area(int read_write, struct inode *inode, struct file *filp,
  557.               loff_t offset, size_t count)
  558. {
  559.     /* Candidates for mandatory locking have the setgid bit set
  560.      * but no group execute bit -  an otherwise meaningless combination.
  561.      */
  562.     if (IS_MANDLOCK(inode) &&
  563.         (inode->i_mode & (S_ISGID | S_IXGRP)) == S_ISGID)
  564.         return (locks_mandatory_area(read_write, inode, filp, offset,
  565.                          count));
  566.     return (0);
  567. }
  568.  
  569. int locks_mandatory_locked(struct inode *inode)
  570. {
  571.     fl_owner_t owner = current->files;
  572.     struct file_lock *fl;
  573.  
  574.     /* Search the lock list for this inode for any POSIX locks.
  575.      */
  576.     for (fl = inode->i_flock; fl != NULL; fl = fl->fl_next) {
  577.         if (!(fl->fl_flags & FL_POSIX))
  578.             continue;
  579.         if (fl->fl_owner != owner)
  580.             return (-EAGAIN);
  581.     }
  582.     return (0);
  583. }
  584.  
  585. int locks_mandatory_area(int read_write, struct inode *inode,
  586.              struct file *filp, loff_t offset,
  587.              size_t count)
  588. {
  589.     struct file_lock *fl;
  590.     struct file_lock tfl;
  591.  
  592.     memset(&tfl, 0, sizeof(tfl));
  593.  
  594.     tfl.fl_file = filp;
  595.     tfl.fl_flags = FL_POSIX | FL_ACCESS;
  596.     tfl.fl_owner = current->files;
  597.     tfl.fl_pid = current->pid;
  598.     tfl.fl_type = (read_write == FLOCK_VERIFY_WRITE) ? F_WRLCK : F_RDLCK;
  599.     tfl.fl_start = offset;
  600.     tfl.fl_end = offset + count - 1;
  601.  
  602. repeat:
  603.     /* Search the lock list for this inode for locks that conflict with
  604.      * the proposed read/write.
  605.      */
  606.     for (fl = inode->i_flock; fl != NULL; fl = fl->fl_next) {
  607.         if (!(fl->fl_flags & FL_POSIX))
  608.             continue;
  609.         /* Block for writes against a "read" lock,
  610.          * and both reads and writes against a "write" lock.
  611.          */
  612.         if (posix_locks_conflict(fl, &tfl)) {
  613.             if (filp && (filp->f_flags & O_NONBLOCK))
  614.                 return (-EAGAIN);
  615.             if (signal_pending(current))
  616.                 return (-ERESTARTSYS);
  617.             if (posix_locks_deadlock(&tfl, fl))
  618.                 return (-EDEADLK);
  619.  
  620.             locks_insert_block(fl, &tfl);
  621.             interruptible_sleep_on(&tfl.fl_wait);
  622.             locks_delete_block(fl, &tfl);
  623.  
  624.             if (signal_pending(current))
  625.                 return (-ERESTARTSYS);
  626.             /* If we've been sleeping someone might have
  627.              * changed the permissions behind our back.
  628.              */
  629.             if ((inode->i_mode & (S_ISGID | S_IXGRP)) != S_ISGID)
  630.                 break;
  631.             goto repeat;
  632.         }
  633.     }
  634.     return (0);
  635. }
  636.  
  637. /* Verify a "struct flock" and copy it to a "struct file_lock" as a POSIX
  638.  * style lock.
  639.  */
  640. static int posix_make_lock(struct file *filp, struct file_lock *fl,
  641.                struct flock *l)
  642. {
  643.     off_t start;
  644.  
  645.     memset(fl, 0, sizeof(*fl));
  646.     
  647.     fl->fl_flags = FL_POSIX;
  648.  
  649.     switch (l->l_type) {
  650.     case F_RDLCK:
  651.     case F_WRLCK:
  652.     case F_UNLCK:
  653.         fl->fl_type = l->l_type;
  654.         break;
  655.     default:
  656.         return (0);
  657.     }
  658.  
  659.     switch (l->l_whence) {
  660.     case 0: /*SEEK_SET*/
  661.         start = 0;
  662.         break;
  663.     case 1: /*SEEK_CUR*/
  664.         start = filp->f_pos;
  665.         break;
  666.     case 2: /*SEEK_END*/
  667.         start = filp->f_dentry->d_inode->i_size;
  668.         break;
  669.     default:
  670.         return (0);
  671.     }
  672.  
  673.     if (((start += l->l_start) < 0) || (l->l_len < 0))
  674.         return (0);
  675.     fl->fl_start = start;    /* we record the absolute position */
  676.     if ((l->l_len == 0) || ((fl->fl_end = start + l->l_len - 1) < 0))
  677.         fl->fl_end = OFFSET_MAX;
  678.     
  679.     fl->fl_file = filp;
  680.     fl->fl_owner = current->files;
  681.     fl->fl_pid = current->pid;
  682.  
  683.     return (1);
  684. }
  685.  
  686. /* Verify a call to flock() and fill in a file_lock structure with
  687.  * an appropriate FLOCK lock.
  688.  */
  689. static int flock_make_lock(struct file *filp, struct file_lock *fl,
  690.                unsigned int cmd)
  691. {
  692.     memset(fl, 0, sizeof(*fl));
  693.  
  694.     if (!filp->f_dentry)    /* just in case */
  695.         return (0);
  696.  
  697.     switch (cmd & ~LOCK_NB) {
  698.     case LOCK_SH:
  699.         fl->fl_type = F_RDLCK;
  700.         break;
  701.     case LOCK_EX:
  702.         fl->fl_type = F_WRLCK;
  703.         break;
  704.     case LOCK_UN:
  705.         fl->fl_type = F_UNLCK;
  706.         break;
  707.     default:
  708.         return (0);
  709.     }
  710.  
  711.     fl->fl_flags = FL_FLOCK;
  712.     fl->fl_start = 0;
  713.     fl->fl_end = OFFSET_MAX;
  714.     fl->fl_file = filp;
  715.     fl->fl_owner = NULL;
  716.     
  717.     return (1);
  718. }
  719.  
  720. /* Determine if lock sys_fl blocks lock caller_fl. POSIX specific
  721.  * checking before calling the locks_conflict().
  722.  */
  723. static int posix_locks_conflict(struct file_lock *caller_fl, struct file_lock *sys_fl)
  724. {
  725.     /* POSIX locks owned by the same process do not conflict with
  726.      * each other.
  727.      */
  728.     if (!(sys_fl->fl_flags & FL_POSIX) ||
  729.         locks_same_owner(caller_fl, sys_fl))
  730.         return (0);
  731.  
  732.     return (locks_conflict(caller_fl, sys_fl));
  733. }
  734.  
  735. /* Determine if lock sys_fl blocks lock caller_fl. FLOCK specific
  736.  * checking before calling the locks_conflict().
  737.  */
  738. static int flock_locks_conflict(struct file_lock *caller_fl, struct file_lock *sys_fl)
  739. {
  740.     /* FLOCK locks referring to the same filp do not conflict with
  741.      * each other.
  742.      */
  743.     if (!(sys_fl->fl_flags & FL_FLOCK) ||
  744.         (caller_fl->fl_file == sys_fl->fl_file))
  745.         return (0);
  746.  
  747.     return (locks_conflict(caller_fl, sys_fl));
  748. }
  749.  
  750. /* Determine if lock sys_fl blocks lock caller_fl. Common functionality
  751.  * checks for overlapping locks and shared/exclusive status.
  752.  */
  753. static int locks_conflict(struct file_lock *caller_fl, struct file_lock *sys_fl)
  754. {
  755.     if (!locks_overlap(caller_fl, sys_fl))
  756.         return (0);
  757.  
  758.     switch (caller_fl->fl_type) {
  759.     case F_RDLCK:
  760.         return (sys_fl->fl_type == F_WRLCK);
  761.         
  762.     case F_WRLCK:
  763.         return (1);
  764.  
  765.     default:
  766.         printk("locks_conflict(): impossible lock type - %d\n",
  767.                caller_fl->fl_type);
  768.         break;
  769.     }
  770.     return (0);    /* This should never happen */
  771. }
  772.  
  773. /* This function tests for deadlock condition before putting a process to
  774.  * sleep. The detection scheme is no longer recursive. Recursive was neat,
  775.  * but dangerous - we risked stack corruption if the lock data was bad, or
  776.  * if the recursion was too deep for any other reason.
  777.  *
  778.  * We rely on the fact that a task can only be on one lock's wait queue
  779.  * at a time. When we find blocked_task on a wait queue we can re-search
  780.  * with blocked_task equal to that queue's owner, until either blocked_task
  781.  * isn't found, or blocked_task is found on a queue owned by my_task.
  782.  *
  783.  * Note: the above assumption may not be true when handling lock requests
  784.  * from a broken NFS client. But broken NFS clients have a lot more to
  785.  * worry about than proper deadlock detection anyway... --okir
  786.  */
  787. static int posix_locks_deadlock(struct file_lock *caller_fl,
  788.                 struct file_lock *block_fl)
  789. {
  790.     struct file_lock *fl;
  791.     struct file_lock *bfl;
  792.     void         *caller_owner, *blocked_owner;
  793.     unsigned int     caller_pid, blocked_pid;
  794.  
  795.     caller_owner = caller_fl->fl_owner;
  796.     caller_pid = caller_fl->fl_pid;
  797.     blocked_owner = block_fl->fl_owner;
  798.     blocked_pid = block_fl->fl_pid;
  799.  
  800. next_task:
  801.     if (caller_owner == blocked_owner && caller_pid == blocked_pid)
  802.         return (1);
  803.     for (fl = file_lock_table; fl != NULL; fl = fl->fl_nextlink) {
  804.         if (fl->fl_owner == NULL || fl->fl_nextblock == NULL)
  805.             continue;
  806.         for (bfl = fl->fl_nextblock; bfl != fl; bfl = bfl->fl_nextblock) {
  807.             if (bfl->fl_owner == blocked_owner &&
  808.                 bfl->fl_pid == blocked_pid) {
  809.                 if (fl->fl_owner == caller_owner &&
  810.                     fl->fl_pid == caller_pid) {
  811.                     return (1);
  812.                 }
  813.                 blocked_owner = fl->fl_owner;
  814.                 blocked_pid = fl->fl_pid;
  815.                 goto next_task;
  816.             }
  817.         }
  818.     }
  819.     return (0);
  820. }
  821.  
  822. /* Try to create a FLOCK lock on filp. We always insert new FLOCK locks at
  823.  * the head of the list, but that's secret knowledge known only to the next
  824.  * two functions.
  825.  */
  826. static int flock_lock_file(struct file *filp, struct file_lock *caller,
  827.                unsigned int wait)
  828. {
  829.     struct file_lock *fl;
  830.     struct file_lock *new_fl = NULL;
  831.     struct file_lock **before;
  832.     struct inode * inode = filp->f_dentry->d_inode;
  833.     int error, change;
  834.     int unlock = (caller->fl_type == F_UNLCK);
  835.  
  836.     /*
  837.      * If we need a new lock, get it in advance to avoid races.
  838.      */
  839.     if (!unlock) {
  840.         error = -ENOLCK;
  841.         new_fl = locks_alloc_lock(caller);
  842.         if (!new_fl)
  843.             goto out;
  844.     }
  845.  
  846.     error = 0;
  847. search:
  848.     change = 0;
  849.     before = &inode->i_flock;
  850.     while (((fl = *before) != NULL) && (fl->fl_flags & FL_FLOCK)) {
  851.         if (caller->fl_file == fl->fl_file) {
  852.             if (caller->fl_type == fl->fl_type)
  853.                 goto out;
  854.             change = 1;
  855.             break;
  856.         }
  857.         before = &fl->fl_next;
  858.     }
  859.     /* change means that we are changing the type of an existing lock, or
  860.      * or else unlocking it.
  861.      */
  862.     if (change) {
  863.         /* N.B. What if the wait argument is false? */
  864.         locks_delete_lock(before, !unlock);
  865.         /*
  866.          * If we waited, another lock may have been added ...
  867.          */
  868.         if (!unlock)
  869.             goto search;
  870.     }
  871.     if (unlock)
  872.         goto out;
  873.  
  874. repeat:
  875.     /* Check signals each time we start */
  876.     error = -ERESTARTSYS;
  877.     if (signal_pending(current))
  878.         goto out;
  879.     for (fl = inode->i_flock; (fl != NULL) && (fl->fl_flags & FL_FLOCK);
  880.          fl = fl->fl_next) {
  881.         if (!flock_locks_conflict(new_fl, fl))
  882.             continue;
  883.         error = -EAGAIN;
  884.         if (!wait)
  885.             goto out;
  886.         locks_insert_block(fl, new_fl);
  887.         interruptible_sleep_on(&new_fl->fl_wait);
  888.         locks_delete_block(fl, new_fl);
  889.         goto repeat;
  890.     }
  891.     locks_insert_lock(&inode->i_flock, new_fl);
  892.     new_fl = NULL;
  893.     error = 0;
  894.  
  895. out:
  896.     if (new_fl)
  897.         locks_free_lock(new_fl);
  898.     return error;
  899. }
  900.  
  901. /* Add a POSIX style lock to a file.
  902.  * We merge adjacent locks whenever possible. POSIX locks are sorted by owner
  903.  * task, then by starting address
  904.  *
  905.  * Kai Petzke writes:
  906.  * To make freeing a lock much faster, we keep a pointer to the lock before the
  907.  * actual one. But the real gain of the new coding was, that lock_it() and
  908.  * unlock_it() became one function.
  909.  *
  910.  * To all purists: Yes, I use a few goto's. Just pass on to the next function.
  911.  */
  912.  
  913. int posix_lock_file(struct file *filp, struct file_lock *caller,
  914.                unsigned int wait)
  915. {
  916.     struct file_lock *fl;
  917.     struct file_lock *new_fl, *new_fl2;
  918.     struct file_lock *left = NULL;
  919.     struct file_lock *right = NULL;
  920.     struct file_lock **before;
  921.     struct inode * inode = filp->f_dentry->d_inode;
  922.     int error, added = 0;
  923.  
  924.     /*
  925.      * We may need two file_lock structures for this operation,
  926.      * so we get them in advance to avoid races.
  927.      */
  928.     new_fl  = locks_empty_lock();
  929.     new_fl2 = locks_empty_lock();
  930.     error = -ENOLCK; /* "no luck" */
  931.     if (!(new_fl && new_fl2))
  932.         goto out;
  933.  
  934.     if (caller->fl_type != F_UNLCK) {
  935.   repeat:
  936.         for (fl = inode->i_flock; fl != NULL; fl = fl->fl_next) {
  937.             if (!(fl->fl_flags & FL_POSIX))
  938.                 continue;
  939.             if (!posix_locks_conflict(caller, fl))
  940.                 continue;
  941.             error = -EAGAIN;
  942.             if (!wait)
  943.                 goto out;
  944.             error = -EDEADLK;
  945.             if (posix_locks_deadlock(caller, fl))
  946.                 goto out;
  947.             error = -ERESTARTSYS;
  948.             if (signal_pending(current))
  949.                 goto out;
  950.             locks_insert_block(fl, caller);
  951.             interruptible_sleep_on(&caller->fl_wait);
  952.             locks_delete_block(fl, caller);
  953.             goto repeat;
  954.           }
  955.       }
  956.  
  957.     /*
  958.      * We've allocated the new locks in advance, so there are no
  959.      * errors possible (and no blocking operations) from here on.
  960.      * 
  961.      * Find the first old lock with the same owner as the new lock.
  962.      */
  963.     
  964.     before = &inode->i_flock;
  965.  
  966.     /* First skip locks owned by other processes.
  967.      */
  968.     while ((fl = *before) && (!(fl->fl_flags & FL_POSIX) ||
  969.                   !locks_same_owner(caller, fl))) {
  970.         before = &fl->fl_next;
  971.     }
  972.  
  973.     /* Process locks with this owner.
  974.      */
  975.     while ((fl = *before) && locks_same_owner(caller, fl)) {
  976.         /* Detect adjacent or overlapping regions (if same lock type)
  977.          */
  978.         if (caller->fl_type == fl->fl_type) {
  979.             if (fl->fl_end < caller->fl_start - 1)
  980.                 goto next_lock;
  981.             /* If the next lock in the list has entirely bigger
  982.              * addresses than the new one, insert the lock here.
  983.              */
  984.             if (fl->fl_start > caller->fl_end + 1)
  985.                 break;
  986.  
  987.             /* If we come here, the new and old lock are of the
  988.              * same type and adjacent or overlapping. Make one
  989.              * lock yielding from the lower start address of both
  990.              * locks to the higher end address.
  991.              */
  992.             if (fl->fl_start > caller->fl_start)
  993.                 fl->fl_start = caller->fl_start;
  994.             else
  995.                 caller->fl_start = fl->fl_start;
  996.             if (fl->fl_end < caller->fl_end)
  997.                 fl->fl_end = caller->fl_end;
  998.             else
  999.                 caller->fl_end = fl->fl_end;
  1000.             if (added) {
  1001.                 locks_delete_lock(before, 0);
  1002.                 continue;
  1003.             }
  1004.             caller = fl;
  1005.             added = 1;
  1006.         }
  1007.         else {
  1008.             /* Processing for different lock types is a bit
  1009.              * more complex.
  1010.              */
  1011.             if (fl->fl_end < caller->fl_start)
  1012.                 goto next_lock;
  1013.             if (fl->fl_start > caller->fl_end)
  1014.                 break;
  1015.             if (caller->fl_type == F_UNLCK)
  1016.                 added = 1;
  1017.             if (fl->fl_start < caller->fl_start)
  1018.                 left = fl;
  1019.             /* If the next lock in the list has a higher end
  1020.              * address than the new one, insert the new one here.
  1021.              */
  1022.             if (fl->fl_end > caller->fl_end) {
  1023.                 right = fl;
  1024.                 break;
  1025.             }
  1026.             if (fl->fl_start >= caller->fl_start) {
  1027.                 /* The new lock completely replaces an old
  1028.                  * one (This may happen several times).
  1029.                  */
  1030.                 if (added) {
  1031.                     locks_delete_lock(before, 0);
  1032.                     continue;
  1033.                 }
  1034.                 /* Replace the old lock with the new one.
  1035.                  * Wake up anybody waiting for the old one,
  1036.                  * as the change in lock type might satisfy
  1037.                  * their needs.
  1038.                  */
  1039.                 locks_wake_up_blocks(fl, 0);
  1040.                 fl->fl_start = caller->fl_start;
  1041.                 fl->fl_end = caller->fl_end;
  1042.                 fl->fl_type = caller->fl_type;
  1043.                 fl->fl_u = caller->fl_u;
  1044.                 caller = fl;
  1045.                 added = 1;
  1046.             }
  1047.         }
  1048.         /* Go on to next lock.
  1049.          */
  1050.     next_lock:
  1051.         before = &fl->fl_next;
  1052.     }
  1053.  
  1054.     error = 0;
  1055.     if (!added) {
  1056.         if (caller->fl_type == F_UNLCK)
  1057.             goto out;
  1058.         locks_init_lock(new_fl, caller);
  1059.         locks_insert_lock(before, new_fl);
  1060.         new_fl = NULL;
  1061.     }
  1062.     if (right) {
  1063.         if (left == right) {
  1064.             /* The new lock breaks the old one in two pieces,
  1065.              * so we have to use the second new lock (in this
  1066.              * case, even F_UNLCK may fail!).
  1067.              */
  1068.             left = locks_init_lock(new_fl2, right);
  1069.             locks_insert_lock(before, left);
  1070.             new_fl2 = NULL;
  1071.         }
  1072.         right->fl_start = caller->fl_end + 1;
  1073.         locks_wake_up_blocks(right, 0);
  1074.     }
  1075.     if (left) {
  1076.         left->fl_end = caller->fl_start - 1;
  1077.         locks_wake_up_blocks(left, 0);
  1078.     }
  1079. out:
  1080.     /*
  1081.      * Free any unused locks.  (They haven't
  1082.      * ever been used, so we use kfree().)
  1083.      */
  1084.     if (new_fl)
  1085.         kfree(new_fl);
  1086.     if (new_fl2)
  1087.         kfree(new_fl2);
  1088.     return error;
  1089. }
  1090.  
  1091. /*
  1092.  * Allocate an empty lock structure. We can use GFP_KERNEL now that
  1093.  * all allocations are done in advance.
  1094.  */
  1095. static struct file_lock *locks_empty_lock(void)
  1096. {
  1097.     /* Okay, let's make a new file_lock structure... */
  1098.     return ((struct file_lock *) kmalloc(sizeof(struct file_lock),
  1099.                         GFP_KERNEL));
  1100. }
  1101.  
  1102. /*
  1103.  * Initialize a new lock from an existing file_lock structure.
  1104.  */
  1105. static struct file_lock *locks_init_lock(struct file_lock *new,
  1106.                      struct file_lock *fl)
  1107. {
  1108.     if (new) {
  1109.         memset(new, 0, sizeof(*new));
  1110.         new->fl_owner = fl->fl_owner;
  1111.         new->fl_pid = fl->fl_pid;
  1112.         new->fl_file = fl->fl_file;
  1113.         new->fl_flags = fl->fl_flags;
  1114.         new->fl_type = fl->fl_type;
  1115.         new->fl_start = fl->fl_start;
  1116.         new->fl_end = fl->fl_end;
  1117.         new->fl_notify = fl->fl_notify;
  1118.         new->fl_u = fl->fl_u;
  1119.     }
  1120.     return new;
  1121. }
  1122.  
  1123. /* Insert file lock fl into an inode's lock list at the position indicated
  1124.  * by pos. At the same time add the lock to the global file lock list.
  1125.  */
  1126. static void locks_insert_lock(struct file_lock **pos, struct file_lock *fl)
  1127. {
  1128.     fl->fl_nextlink = file_lock_table;
  1129.     fl->fl_prevlink = NULL;
  1130.     if (file_lock_table != NULL)
  1131.         file_lock_table->fl_prevlink = fl;
  1132.     file_lock_table = fl;
  1133.     fl->fl_next = *pos;    /* insert into file's list */
  1134.     *pos = fl;
  1135.  
  1136.     return;
  1137. }
  1138.  
  1139. /* Delete a lock and free it.
  1140.  * First remove our lock from the active lock lists. Then call
  1141.  * locks_wake_up_blocks() to wake up processes that are blocked
  1142.  * waiting for this lock. Finally free the lock structure.
  1143.  */
  1144. static void locks_delete_lock(struct file_lock **thisfl_p, unsigned int wait)
  1145. {
  1146.     struct file_lock *thisfl;
  1147.     struct file_lock *prevfl;
  1148.     struct file_lock *nextfl;
  1149.     
  1150.     thisfl = *thisfl_p;
  1151.     *thisfl_p = thisfl->fl_next;
  1152.  
  1153.     prevfl = thisfl->fl_prevlink;
  1154.     nextfl = thisfl->fl_nextlink;
  1155.  
  1156.     if (nextfl != NULL)
  1157.         nextfl->fl_prevlink = prevfl;
  1158.  
  1159.     if (prevfl != NULL)
  1160.         prevfl->fl_nextlink = nextfl;
  1161.     else
  1162.         file_lock_table = nextfl;
  1163.     
  1164.     locks_wake_up_blocks(thisfl, wait);
  1165.     locks_free_lock(thisfl);
  1166.  
  1167.     return;
  1168. }
  1169.  
  1170.  
  1171. static char *lock_get_status(struct file_lock *fl, int id, char *pfx)
  1172. {
  1173.     static char temp[155];
  1174.     char *p = temp;
  1175.     struct inode *inode;
  1176.  
  1177.     inode = fl->fl_file->f_dentry->d_inode;
  1178.  
  1179.     p += sprintf(p, "%d:%s ", id, pfx);
  1180.     if (fl->fl_flags & FL_POSIX) {
  1181.         p += sprintf(p, "%6s %s ",
  1182.                  (fl->fl_flags & FL_ACCESS) ? "ACCESS" : "POSIX ",
  1183.                  (IS_MANDLOCK(inode) &&
  1184.                   (inode->i_mode & (S_IXGRP | S_ISGID)) == S_ISGID) ?
  1185.                  "MANDATORY" : "ADVISORY ");
  1186.     }
  1187.     else {
  1188.         p += sprintf(p, "FLOCK  ADVISORY  ");
  1189.     }
  1190.     p += sprintf(p, "%s ", (fl->fl_type == F_RDLCK) ? "READ " : "WRITE");
  1191.     p += sprintf(p, "%d %s:%ld %ld %ld ",
  1192.              fl->fl_pid,
  1193.              kdevname(inode->i_dev), inode->i_ino, fl->fl_start,
  1194.              fl->fl_end);
  1195.     sprintf(p, "%08lx %08lx %08lx %08lx %08lx\n",
  1196.         (long)fl, (long)fl->fl_prevlink, (long)fl->fl_nextlink,
  1197.         (long)fl->fl_next, (long)fl->fl_nextblock);
  1198.     return (temp);
  1199. }
  1200.  
  1201. static inline int copy_lock_status(char *p, char **q, off_t pos, int len,
  1202.                    off_t offset, off_t length)
  1203. {
  1204.     off_t i;
  1205.  
  1206.     i = pos - offset;
  1207.     if (i > 0) {
  1208.         if (i >= length) {
  1209.             i = len + length - i;
  1210.             memcpy(*q, p, i);
  1211.             *q += i;
  1212.             return (0);
  1213.         }
  1214.         if (i < len) {
  1215.             p += len - i;
  1216.         }
  1217.         else
  1218.             i = len;
  1219.         memcpy(*q, p, i);
  1220.         *q += i;
  1221.     }
  1222.     
  1223.     return (1);
  1224. }
  1225.  
  1226. int get_locks_status(char *buffer, char **start, off_t offset, off_t length)
  1227. {
  1228.     struct file_lock *fl;
  1229.     struct file_lock *bfl;
  1230.     char *p;
  1231.     char *q = buffer;
  1232.     off_t i, len, pos = 0;
  1233.  
  1234.     for (fl = file_lock_table, i = 1; fl != NULL; fl = fl->fl_nextlink, i++) {
  1235.         p = lock_get_status(fl, i, "");
  1236.         len = strlen(p);
  1237.         pos += len;
  1238.         if (!copy_lock_status(p, &q, pos, len, offset, length))
  1239.             goto done;
  1240.         if ((bfl = fl->fl_nextblock) == NULL)
  1241.             continue;
  1242.         do {
  1243.             p = lock_get_status(bfl, i, " ->");
  1244.             len = strlen(p);
  1245.             pos += len;
  1246.             if (!copy_lock_status(p, &q, pos, len, offset, length))
  1247.                 goto done;
  1248.         } while ((bfl = bfl->fl_nextblock) != fl);
  1249.     }
  1250. done:
  1251.     if (q != buffer)
  1252.         *start = buffer;
  1253.     return (q - buffer);
  1254. }
  1255.  
  1256.  
  1257.  
  1258.